package com.itheima.controller;



import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.utils.POIUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/names")
    public Result getManagerNames() {
        List<Map<String, Object>> list = memberService.findManagerByRoleId();
        return  Result.success( list,"查询健康管理师成功");
    }

    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=memberService.findPage(queryPageBean);
        return pageResult;
    }

    @PostMapping
    public Result add(@RequestBody Member member){
        memberService.add(member);
        return Result.success("新增会员成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        memberService.deleteById(id);
        return Result.success("删除会员成功");
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Member byId = memberService.findById(id);
        return Result.success(byId, MessageConstant.QUERY_USER_SUCCESS);
    }

    @PutMapping
    public Result edit(@RequestBody Member member){
        memberService.edit(member);
        return Result.success("编辑会员成功");
    }

    @GetMapping("/exportExcel")
    public Result findNames(HttpServletResponse response,HttpServletRequest request) throws IOException {
        try {
      //      String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member.xlsx";
          //  String filePath ="D:\\itheima_health\\heima_health_backend\\src\\main\\resources\\static\\template\\member.xlsx";
            //基于提供的Excel模板文件在内存中创建一个Excel表格对象
//            ClassPathResource classPathResource = new ClassPathResource("static/template/member.xlsx");
//            String filePath = classPathResource.getURL().getPath();

            XSSFWorkbook excel = new XSSFWorkbook();
            //创建第一个工作表
            XSSFSheet sheet = excel.createSheet();
            XSSFRow row1 = sheet.getRow(0);
            if (row1 == null) {
                row1 = sheet.createRow(0);
            }
            if (row1.getCell(0)==null){
                row1.createCell(0);
                row1.getCell(0).setCellValue("档案号");
            }
            if (row1.getCell(1)==null){
                row1.createCell(1);
                row1.getCell(1).setCellValue("姓名");
            }
            if (row1.getCell(2)==null){
                row1.createCell(2);
                row1.getCell(2).setCellValue("性别");
            }
            if (row1.getCell(3)==null){
                row1.createCell(3);
                row1.getCell(3).setCellValue("年龄");
            }
            if (row1.getCell(4)==null){
                row1.createCell(4);
                row1.getCell(4).setCellValue("健康管理师");
            }
            if (row1.getCell(5)==null){
                row1.createCell(5);
                row1.getCell(5).setCellValue("注册时间");
            }
            if (row1.getCell(6)==null){
                row1.createCell(6);
                row1.getCell(6).setCellValue("手机号码");
            }
            if (row1.getCell(7)==null){
                row1.createCell(7);
                row1.getCell(7).setCellValue("套餐");
            }
            List<Member> list = memberService.findAll();
            int j=1;
            for (int i = 0; i <list.size(); i++) {
                XSSFRow row = sheet.getRow(j++);
                if (row == null) {
                    row = sheet.createRow(j-1);
                }
                Member member = list.get(i);
                if (row.getCell(0)==null){
                    row.createCell(0);
                    row.getCell(0).setCellValue(member.getFileNumber());
                }
                if (row.getCell(1)==null){
                    row.createCell(1);
                    row.getCell(1).setCellValue(member.getName());
                }
                if (row.getCell(2)==null){
                    row.createCell(2);
                    row.getCell(2).setCellValue(member.getSex());
                }
                if (row.getCell(4)==null){
                    row.createCell(4);
                    row.getCell(4).setCellValue(member.getManager());
                }
                if (row.getCell(5)==null){
                    row.createCell(5);
                    row.getCell(5).setCellValue(member.getRegTime());
                }
                if (row.getCell(6)==null){
                    row.createCell(6);
                    row.getCell(6).setCellValue(member.getPhoneNumber());
                }
            }

            //使用输出流进行表格下载,基于浏览器作为客户端下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型 content-type  application/vnd.ms-excel
            // response.setHeader("Content-Type","application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=test.xlsx");//指定以附件形式进行下载 强制下载！！
            excel.write(out);

            out.flush();
            out.close();
            excel.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("下载失败");
        }
    }

}
